package com.hoshino.momochi.util;

import java.util.List;
import java.util.Objects;

public class PageCheck<R> {

    private static final Integer PAGE_INFO = 10;

    private List<R> dataList;

    private Integer havePage;

    public List<R> getDataList() {
        return dataList;
    }

    public Integer getHavePage() {
        return havePage;
    }

    public void setDataList(List<R> dataList) {
        this.dataList = dataList;
    }

    public void setHavePage(Integer havePage) {
        this.havePage = havePage;
    }

    /**
     * @param list
     * @param pageIndex 页码
     * @param pageSize  页面大小
     * @param <R>
     * @return
     */
    public static <R> PageCheck<R> pageCheck(List<R> list, Integer pageIndex, Integer pageSize) {
        if (Objects.isNull(pageSize)) {
            pageSize = PAGE_INFO;
        }
        PageCheck<R> res = new PageCheck<>();
        Integer len = list.size();
        if (len == 0) {
            return null;
        }
        if (pageSize.intValue() == -1) {
            pageSize = len;
        }
        Integer have_page = (len + pageSize - 1) / pageSize;
        if (Objects.isNull(pageIndex) || pageIndex <= 0 || pageIndex > have_page) {
            return null;
        }
        res.setDataList(list.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).toList());
        res.setHavePage(have_page);
        return res;
    }

    /**
     * 包装数据
     *
     * @param list
     * @param <R>
     * @return
     */
    public static <R> PageCheck<R> makeData(List<R> list) {
        return pageCheck(list, 1, -1);
    }
}
